package com.lsh.mq;

import com.lsh.container.CommentContainer;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.async.DeferredResult;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 评价消息通知-消费者
 * @author Administrator
 * @date 2019/10/30 22:30
 */
public class CommentConsumer implements MessageListener {

    @Autowired
    CommentContainer commentContainer;

    @Override
    public void onMessage(Message message) {

        try {
            Integer toUserId = (Integer) ((ActiveMQObjectMessage) message).getObject();
            Map<String, DeferredResult> container = commentContainer.getCommentContainer();

            System.out.println(toUserId);
            DeferredResult result = container.get(String.valueOf(toUserId));
            if(result != null) result.setResult("received a comment");
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
